#coding:utf-8
import requests
from lxml import etree
import time
import itertools
import re
import wget
import os
import sys 
reload(sys) 
sys.setdefaultencoding( "utf-8" ) 

from fake_useragent import UserAgent
ua = UserAgent()

class MaiZi(object):
	def __init__(self):
		self.url = ''
		self.html = ''
		self.info = ''
		self.path = ''
	#获取网页源代码
	def Requests_html(self,url):
		#自定义请求头部，该网站不需要登录或者cookie信息也可以获取到数据
		self.headers = {
					'User-Agent':ua.random,
					"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
					"Accept-Encoding":"gzip, deflate, sdch",
					"Accept-Language":"zh-CN,zh;q=0.8",
					"Cache-Control":"max-age=0",
					"Connection":"keep-alive",
					}
		try:
			req = requests.get(url,headers = self.headers)
			return req
		except :
			return False
	#获取课程列表
	def Course_List(self,html):
		#格式化网页结构
		tree = etree.HTML(html)
		#课程目录在li标签下，使用xpath提取li标签
		course_list = tree.xpath('/html/body/div[3]/div/div[3]/ul/li')
		print len(course_list)
		course = []
		#遍历所有的li标签，获取课程详情页面url链接
		for i in range(len(course_list)):
			name = course_list[i].xpath('a/@title')
			print "序号：%d \n课程名：%s"%(i+1,name[0])
			href = course_list[i].xpath('a/@href')
			course.append('http://www.maiziedu.com' + href[0])
		return course
	#交互式页面集成函数
	def Change_Box(self,course_list):
		print "请输入位于%d--%d之间的数字序号\n如需退出，输入0\n"%(1,len(course_list))
		course_num = int(raw_input("\n课程序号 = "))
		if course_num > 0 and course_num < (len(course_list) + 1) :
			#输出对应序号的课程详情页URL链接
			print course_list[course_num-1]
			#提取该课程详情页代码
			course_info_html = self.Requests_html(course_list[course_num-1])
			#调用Chapter_List函数，返回课程章节信息
				#Chapter[0]----课程章节名称
				#Chapter[1]----课程章节对应URL链接
			Chapter = self.Chapter_List(course_info_html)
			#课程其他详细信息
				#course_info[0]----课程名称
				#course_info[1]----课程简介
				#course_info[2]----课程标签分类
				#course_info[3]----课程老师
				#course_info[4]----课程老师简介
				#course_info[5]----课程播放次数
				#course_info[6]----课程交流QQ群
				#course_info[7]----课程图片链接
			course_info = self.Course_Info(course_info_html)
			#课程名称，用来创建文件夹
			course_name = course_info[0]
			image_url = course_info[7]
			print "是否需要下载该视频课程？\n"
			print "\n0:退出\n1：指定章节下载\n2：全部下载\n3：不需要下载并返回上一级目录\n"
			TF = raw_input("请按提示输入:")
			print "\n"
			if int(TF) == 1 :
				#调用Appoint_chapter函数，下载指定章节课程视频
				down = self.Appoint_chapter(Chapter,course_name,course_info,image_url)
				print "下载完了！！！！！---状态码：%s"%down
				return down
			elif int(TF) == 2 :
				print "正在全部下载中！"
				#调用DownLoad_All函数下载该课程所有章节课程
				down = self.DownLoad_All(Chapter,course_name,course_info,image_url)
				print "下载完了！！！！！---状态码：%s"%down
				return down
			elif int(TF) == 0 :
				print "不需要下载，已跳过！"
				return 'True1'
			elif int(TF) == 3 :
			 	print "不需要下载，已跳过！"
				return 'True2'
		elif course_num == 0:
			return False
		else:
			print "输入数值不正确，请重新输入！"
			return True
	#获取课程章节目录
	def Chapter_List(self,html):
		#课程章节信息：
		#		章节目录----URL链接
		chapter = []
		tree = etree.HTML(html.text)
		name_url = tree.xpath('/html/body/div[5]/div[1]/div/div[1]/ul/li')
		for li in name_url:
			chapter_name = li.xpath('a/span[1]/text()')
			chapter_url = li.xpath('a/@href')
			chapter_name = chapter_name[0]
			chapter_url = 'http://www.maiziedu.com' + chapter_url[0]
			chapters = [chapter_name,chapter_url]
			chapter.append(chapters)
		return chapter
	#获取课程相关信息
	def Course_Info(self,html):
		tree = etree.HTML(html.text)
		#课程名
		course_name = tree.xpath('/html/body/div[4]/div[2]/h1/text()')
		#课程简介
		course_jianjie = tree.xpath('/html/body/div[4]/div[2]/p[1]/text()')
		#课程标签分类
		course_tag = tree.xpath('/html/body/div[4]/div[2]/p[2]/a/text()')
		#/html/body/div[4]/div[2]/p[2]/a
		if len(course_tag) == 0 :
			course_tag = ["其他"]
		#讲课老师
		course_teacher = tree.xpath('/html/body/div[5]/div[2]/div[1]/div/h3/text()')
		#讲课老师简介
		course_teacher_jianjie = tree.xpath('/html/body/div[5]/div[2]/div[1]/p/text()')
		if len(course_teacher_jianjie) == 0 :
			course_teacher_jianjie = ["无"]
		#课程播放次数
		course_play_times = tree.xpath('/html/body/div[4]/div[2]/p[3]/span[2]/text()')
		#课程交流QQ群
		course_QQ = tree.xpath('/html/body/div[4]/div[2]/p[4]/a[2]/text()')
		course_QQ = ["-*-".join(itertools.chain(course_QQ)).replace(';','')]
		#课程主页image图片链接
		course_image = tree.xpath('/html/body/div[4]/div[1]/img/@src')
		course_image = ['http://www.maiziedu.com' + course_image[0]]

		course_info  = [course_name[0],course_jianjie[0],course_tag[0],\
						course_teacher[0],course_teacher_jianjie[0],\
						course_play_times[0],course_QQ[0],course_image[0]]
		return course_info
	#指定下载章节交互对话框
	def Appoint_chapter(self,Chapter,course_name,course_info,image_url):
		print "\n是否查看课程目录？\n1:查看\n2:不需要查看\n"
		Y_N = raw_input("请根据指定输入：")
		if int(Y_N) == 1 :
			for i in range(len(Chapter)):
				print "章节名称：%s"%Chapter[i][0]
				print "链接：%s"%Chapter[i][1]
		chapter_num = raw_input("\n请输入下载章节序号：")
		chapter_num = int(chapter_num)
		down_url = Chapter[chapter_num-1][1]
		down_chapter_name = Chapter[chapter_num-1][0]
		print "当前下载章节名称：%s"%down_chapter_name
		print "***********"
		print "当前下载链接：%s"%down_url
		print "***********"
		path1 = course_name
		path2 = down_chapter_name
		image_url = image_url
		down = self.Download_mp4(down_url,path1,path2)
		down2 = self.download_Image(image_url,path1,path2)
		self.Save_Course_Info(course_info,path1,path2)
		return down
	#下载视频
		#path1参数为指定文件夹下，课程文件夹名
		#path2参数为指定文件夹下，课程MP4文件名
	def Download_mp4(self,url,path1,path2):
		print "请求链接：%s"%url
		try:
			html = self.Requests_html(url)
			com = re.compile(r'lessonUrl = "http://newoss\.maiziedu\.com/.*?\.mp4')
			urls = re.findall(com,html.text)
			print urls
			#创建文件夹，用path参数命名
			path1 = path1.replace('\\','')
			savepath = "E:\\jiaocheng\\F\\Project\\maizi_edu\\Course\\" + path1
			if(os.path.exists(savepath) == False):
				os.makedirs(savepath)
			savepath = savepath + "\\"
			down_urls = urls[0][13:]
			print "下载链接：%s"%down_urls
			wget.download(down_urls,str(savepath) + path2 + '.mp4')
			print "我的视频下载完了哦！"
			time.sleep(5)
			return True
		except :
			return False
	#保存课程相关信息数据
	def Save_Course_Info(self,course_info,path1,path2):
		try:
			#创建文件夹，用path参数命名
			path1 = path1.replace('\\','')
			savepath = "E:\\jiaocheng\\F\\Project\\maizi_edu\\Course\\" + path1
			if(os.path.exists(savepath) == False):
				os.makedirs(savepath)
			savepath = savepath + "\\"
			with open(str(savepath) + path2 + '.txt','a') as file:
				for info in course_info:
					print info
					file.write(info + '\n')
			file.close()
			return True
		except :
			return False
	#全部下载当前目录课程章节交互对话框
	def DownLoad_All(self,Chapter,course_name,course_info,image_url):
		print "我在下载所有的课程！"
		try:
			for i in range(len(Chapter)):
				down_url = Chapter[i][1]
				print "章节名称：%s"%Chapter[i][0]
				print "链接：%s"%down_url
				path1 = course_name
				print path1
				print "*****************"
				path2 = Chapter[i][0]
				print path2
				down = self.Download_mp4(down_url,path1,path2)
				down2 = self.download_Image(image_url,path1,path2)
			self.Save_Course_Info(course_info,path1,path2)
			return down
		except :
			return False
	#下载当前课程主页图片
	def download_Image(self,url,path1,path2):
		print "我正在开始下载图片了哦！"
		try:
			#创建文件夹，用path参数命名
			path1 = path1.replace('\\','')
			savepath = "E:\\jiaocheng\\F\\Project\\maizi_edu\\Course\\" + path1
			if(os.path.exists(savepath) == False):
				os.makedirs(savepath)
			savepath = savepath + "\\"
			#将图片保存为image
			html = self.Requests_html(image_url)
			f = open(str(savepath) + path1 + '.jpg', 'ab')
			f.write(html.content)
			f.close()
			return True
		except :
			return False



def main():
	#创建类的对象
	maizi = MaiZi()
	while True:
		try:
			i = raw_input("请输入位于1--46之间的页码\n输入其他任意数字结束\npage = ")
			if int(i) < 0 or int(i) > 46 :
				break
			else :
				url = 'http://www.maiziedu.com/course/all-all/0-' + str(i) + '/'
				html = maizi.Requests_html(url)
				#课程详情页链接
				course_list = maizi.Course_List(html.text)
				while True:
					#课程下载交互集成函数
					T_or_F = maizi.Change_Box(course_list)
					if T_or_F == 'True1' :
						print "******************END*******************"
						break
					elif T_or_F == False:
						break
					else:
						continue
		except :
			print "SORRY....小哥哥你的电脑好像奔溃了~~~~"
			print "确保网络连接正确，重新试一下哦~~~~~~"
if __name__ == '__main__':
	main()